package com.example.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hospital.pojo.*;
import com.example.hospital.service.IPatientService;
import com.example.hospital.service.IRbService;
import com.example.hospital.service.IRoService;
import com.example.hospital.service.IRsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "PatientController")
@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private IPatientService iPatientService;
    @Autowired
    private IRsService iRsService;
    @Autowired
    private IRoService iRoService;
    @Autowired
    private IRbService iRbService;
    @ApiOperation(value = "分页查看病人基本资料")
    @GetMapping("/")
    public ResPageBean getPatient(@RequestParam(defaultValue = "1") Integer currentPage,
                                  @RequestParam(defaultValue = "10")Integer size,@RequestParam(defaultValue = "") String keyword){
        return iPatientService.getPatientByPage(currentPage,size,keyword);

    }
    @ApiOperation(value = "添加病人")
    @PostMapping("/addPatient")
    public RespBean addPatient(@RequestBody Patient patient){
        if(iPatientService.save(patient)){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }

    }
    @ApiOperation(value = "根据病人id删除病人")
    @DeleteMapping("/{pid}")
    public RespBean deletePatientByid(@PathVariable Integer pid){
        if(iPatientService.removeById(pid)){
            iRbService.remove(new LambdaQueryWrapper<Rb>().eq(Rb::getPid,pid));
            iRoService.remove(new LambdaQueryWrapper<Ro>().eq(Ro::getPid,pid));
            iRsService.remove(new LambdaQueryWrapper<Rs>().eq(Rs::getPid,pid));
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }

    @ApiOperation(value = "根据病人id查看病人基本信息")
    @GetMapping("/getPatientByid")
    public List<Patient> getPatientByid(@RequestParam Integer pid){
        LambdaQueryWrapper<Patient> lam = new LambdaQueryWrapper<>();
        lam.eq(Patient::getId,pid);
        List<Patient> list = iPatientService.list(lam);
        return list;
    }
    @ApiOperation(value = "根据病人id修改病人基本信息")
    @PutMapping("/updatePatientByid")
    public RespBean updatePatientByid(@RequestBody Patient patient){
       if(iPatientService.updateById(patient)){
           return RespBean.success("修改成功");
       }else {
           return RespBean.error("修改失败");
       }
    }
}
